@extends('layouts.app')

@section('content')
    <div class="container">
        @if (session('success'))
            <div class="alert alert-success" role="alert">
                {{ session('success') }}
            </div>
        @endif
        @if($mall->is_show==0)
            <div class="alert alert-danger" role="alert">
                {{ $mall->name }}当前休息中，你可以逛逛其他商家！
            </div>
        @endif
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="/">首页</a></li>
                <li class="breadcrumb-item"><a href="{{ route('home') }}">个人中心</a></li>
                <li class="breadcrumb-item active" aria-current="page">{{ $mall->name }}购物车</li>
            </ol>
        </nav>
        <div class="row">
            @foreach($carts as $cart)
                <div class="col-md-4">
                    <div class="card" style="width: 92%; margin: 10px;">
                        <div class="card-body">
                            @if(!is_null($cart->product))
                                <h6 class="card-subtitle mb-3">商品：{{ $cart->product->name }}</h6>
                                <h6 class="card-subtitle mb-3">单价：￥{{ sprintf("%01.2f",$cart->product->money) }}</h6>
                                <h6 class="card-subtitle mb-3">数量：{{ $cart->total_num }}</h6>
                                <span class="badge badge-danger"
                                      style="margin-right: 5px;font-size: 85%!important;">合计金额：￥{{ sprintf("%01.2f",$cart->total_num * $cart->product->money) }}</span>
                            @else
                                <h6 class="card-subtitle mb-4">商品:此无效商品请删除</h6>
                            @endif
                        </div>
                        <div class="card-footer bg-transparent ">
                            <form action="{{ route('cart.destroy',$cart->id) }}" method="post"
                                  style="float:left;margin-right: 10px;">
                                @csrf
                                @method('delete')
                                <button type="submit" class="btn btn-dark btn-sm">删除</button>
                            </form>
                        </div>
                    </div>
                </div>
            @endforeach

            <div class="col-md-12">
                <div class="card-body">
                    <div class="card-header">总金额：￥{{ sprintf("%01.2f",$total_price) }}</div>
                    <div class="card-header">
                        <a href="{{ route('order.create',$mall->id) }}">
                            <button type="button" class="btn btn-primary">继续购物</button>
                        </a>
                        @if($mall->is_show==0)
                            @if($is_admin)
                                <button type="button" class="btn btn-danger" data-toggle="modal"
                                        data-target="#staticBackdrop"  @if($carts_is_empty) disabled @endif>√
                                </button>
                            @endif
                            <button type="button" class="btn btn-warning" disabled>商家休息中</button>
                        @endif
                        @if($mall->is_show==1)
                            <button type="button" class="btn btn-danger" data-toggle="modal" @if($carts_is_empty) disabled @endif
                                    data-target="#staticBackdrop">提交订单
                            </button>
                        @endif
                        @if($is_admin)
                            <button class="btn btn-success" type="button" id="open_proxy">开启代买</button>
                            <div class="form-inline" style="margin-top:15px;display: none;" id="proxy_content">
                                <label for="proxy_buy" style="margin-right:10px;">代买对象</label>
                                <select class="form-control" id="proxy_buy" name="aged_id">
                                    @foreach($ageds as $aged)
                                        <option value="{{ $aged->id }}">#{{ $aged->id }} | {{ $aged->name }}
                                            | {{ $aged->tel }}
                                            | {{ $aged->address }} | {{ $aged->note }}</option>
                                    @endforeach
                                </select>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="staticBackdrop" data-backdrop="static" tabindex="-1" role="dialog"
         aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="staticBackdropLabel">重要提示</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <p>1、此团购服务仅为本小区在住业主服务，不允许帮其他小区业主购买。（菜品每个社区都是限量供应）</p>
                    <p>2、部分商品缺货商超会自动替换等值商品，恕不另行通知。</p>
                    <p>*违背第1条我们会冻结你的账户，拒绝为你提供服务。</p>
                    <p>*如果你同意以上约定，请点击【我同意】，否则请点击【我不同意】退出购物。</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">我不同意</button>
                    <form action="{{ route('order.store') }}" method="post" class="submit">
                        @csrf
                        <input type="hidden" value="{{ $mall->id }}" name="mall_id">
                        <input type="hidden" value="0" name="aged_id" id="proxy_buy_value">
                        @if($mall->is_show==1)
                            <button type="submit" class="btn btn-primary submit-on"  @if($carts_is_empty) disabled @endif>我同意</button>
                        @endif
                        @if($mall->is_show==0 && $is_admin)
                            <button type="submit" class="btn btn-primary"  @if($carts_is_empty) disabled @endif>√</button>
                        @endif
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
    <script type="text/javascript">
        $(function () {
            let proxy = 0;
            //开启&关闭代买
            $("#open_proxy").click(function () {
                if (proxy === 0) {
                    $("#proxy_content").show(200);
                    let id = $("#proxy_buy").val();
                    $("#proxy_buy_value").val(id);
                    proxy = 1;
                    $(this).text('关闭代买').addClass("btn-dark").removeClass("btn-success");
                    return false;
                }
                if (proxy === 1) {
                    $("#proxy_content").hide(200);
                    $("#proxy_buy_value").val(0);
                    proxy = 0;
                    $(this).text('开启代买').addClass("btn-success").removeClass("btn-dark");
                    return false;
                }

            });

            //选择代买对象
            $("#proxy_buy").change(function () {
                let id = $(this).val();
                $("#proxy_buy_value").val(id);
            });
        })
    </script>
@endsection

